package com.wind.stream;

import java.util.ArrayList;
import java.util.DoubleSummaryStatistics;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Stream的使用：统计
 *
 * @author Wind
 * @date 2021-06-09
 */
public class Test08Stream {
    public static void main(String[] args) {

        List<Person> persons = new ArrayList<>();
        persons.add(new Person("张三", 7000, 34, "男", "陕西"));
        persons.add(new Person("李四", 8000, 24, "男", "河北"));
        persons.add(new Person("王五", 6000, 42, "女", "甘肃"));
        persons.add(new Person("王五", 9000, 12, "女", "上海"));

        // 求总数
        Long count = persons.stream().collect(Collectors.counting());
        System.out.println(count);

        // 求平均工资
        Double average = persons.stream().collect(Collectors.averagingDouble(Person::getSalary));
        System.out.println(average);

        // 求最高工资
        Integer max = persons.stream().map(t -> t.getSalary()).collect(Collectors.maxBy(Integer::compareTo)).get();
        System.out.println(max);

        // 求工资之和
        Double sum = persons.stream().collect(Collectors.summingDouble(Person::getSalary));
        System.out.println(sum);

        // 一次性统计所有（总结）
        DoubleSummaryStatistics summarizing = persons.stream().collect(Collectors.summarizingDouble(Person::getSalary));
        System.out.println(summarizing);    // DoubleSummaryStatistics{count=4, sum=30000.000000, min=6000.000000, average=7500.000000, max=9000.000000}
        System.out.println(summarizing.getCount());
        System.out.println(summarizing.getSum());
        System.out.println(summarizing.getMin());
        System.out.println(summarizing.getAverage());
        System.out.println(summarizing.getMax());
    }
}
